<template>
    
    <cc-dialog ref="self" title="选择业务归属机构" width="600px" show-confirm @on-confirm="onComcodeConfirm">
        <div style="padding:10px 10px 0 10px">
            <cc-query>
                <cc-query-input :span="10" name="机构代码" v-model="comcodeParam.comCode"></cc-query-input> 
                <cc-query-input :span="10" name="机构名称" v-model="comcodeParam.comName"></cc-query-input> 
                <el-col :span="4">
                    <el-button size="mini" style="margin-top:9px;" type="primary" icon="el-icon-delete" @click="onQuery">
                        查询
                    </el-button>
                </el-col>
            </cc-query>

            <cc-table :rows="comcodeRows" v-model="currentCodeRow" id-name="comcode" :lazy="lazy" @load="load">
                <cc-table-column type="index"  width="60"></cc-table-column>
                <cc-table-column prop="comcode" label="业务归属机构代码" width="150" ></cc-table-column>
                <cc-table-column prop="comcname" label="业务归属机构名称" width="350" ></cc-table-column>
            </cc-table>
        </div>
    </cc-dialog>
    
</template>

<script>
module.exports = {
    data: function() {
        return {       
            lazy: true,
            comcodeParam: {},
            comcodeRows: [],
            currentCodeRow: {}
        }
    },
    props: {
        value: Object
    },
    created: function() {
    },
    methods: {
        open: function() {
            this.queryComcode(() => {
                this.$refs.self.open();
            });
        },
        filterData: function(data, param) {
            if(!(Util.isBlank(param.comcode) && Util.isBlank(param.comcname))) {
                for(var i in data) {
                    var item = data[i];
                    item.hasChildren = false;
                }
            }
        },
        queryComcodeByUp: function(up, _call, isLoad) {
            var url = "../comcode/loadAllCompany";
            var param = {
                upperComCode: up,
                comcode: this.comcodeParam.comCode,
                comcname: this.comcodeParam.comName
            };
            
            if(isLoad) {
                UI.post(url, param, (data) => {
                    this.filterData(data, param);
                    _call(data);
                });
            } else {
                $.post(url, param, (data) => {
                    this.filterData(data, param);
                    _call(data);
                });
            }
        },
        onQuery: function() {
            this.queryComcode();
        },
        queryComcode: function(_success) {
            this.queryComcodeByUp('00000000', (data) => {
                // console.log('条件查询', data);
                this.comcodeRows = data;
                if(_success != null) {
                    _success();
                }
            }, true)
        },
        load(tree, treeNode, resolve) {
            this.queryComcodeByUp(tree.comcode, (data) => {
                resolve(data);
            })
        },
        onComcodeConfirm() {
            // 确认后赋值到 编辑框
            var value = Util.clone( this.value );
            value.comCode = this.currentCodeRow.comcode;
            value.comName = this.currentCodeRow.comcname;
            this.$emit('input', value);
            this.$emit('confirm', value);
        }
    },
}
</script>